<?php

class Search{
	private $departureCity;
	private $destinationCity;
	private $departureDate;
	private $returnDate;
	private $adults;
	private $children;
	private $infants;
	private $retour;
	
	private $allTickets;
	
	public function Search($deptCity,$destCity,$deptDate,$retDate,$adults,$children,$infants,$retour){
		if(!$deptCity)$deptCity = "Amsterdam - Luchthaven Schiphol, Nederland (AMS)";
		
		$this->departureCity = $deptCity;
		$this->destinationCity = $destCity;
		$this->departureDate = $deptDate;
		$this->adults = $adults;
		$this->children = $children;
		$this->infants = $infants;
	
		
		if($retDate){
			$this->returnDate = $retDate;
			$this->retour = true;
		}else{ 
			$this->returnDate = false; 
			$this->retour = false;
		}
	}
	
	public function getDepartureCity(){
		return $this->departureCity;
	}
	
	public function getDestinationCity(){
		return $this->destinationCity;
	}
	
	public function getDepartureDate(){
		return $this->departureDate;
	}
	
	public function getReturnDate(){
		return $this->returnDate;
	}
	
	public function getAdults(){
		return $this->adults;
	}
		
	public function getChildren(){
		return $this->children;
	}
	
	public function getInfants(){
		return $this->infants;
	}
	
	public function getRetour(){
		return $this->retour;
	}
	
	public function getDeparturyCityIATA(){
		return $this->getIATACode($this->departureCity);
	}
	
	public function getDestinationCityIATA(){
		return $this->getIATACode($this->destinationCity);
	}
	private function getIATACode($city){
		$string = explode( '(', $city) ;
		$string = substr_replace($string ,"",-1);
		return $string[1];	
	}
	
	public function totalPassengers(){
		return $this->children + $this->infants + $this->adults;
	}
	
	private function getCurrentUrl(){
		 $pageURL = 'http';
		 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
		 $pageURL .= "://";
		 if ($_SERVER["SERVER_PORT"] != "80") {
		  $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
		 } else {
		  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
		 }
		 return $pageURL;
	}
	
	
	
	public function getDay($type,$day){
		// 0, 0 = eerste element van enkeltje
		// 0,1 =  tweede element van enkeltje
		// etc
		
		//todo, functie maken ctrl c + ctrl v is not done <--
		$url = "";
		if(($type) && ($day)){
			// 1,1
			// enkeltje dag +1 
			$x = $this->getCurrentUrl();
			$pieces = explode("&", $x);
			$deptDate = $pieces[2];
			$y =  explode("=", $deptDate);
			$deptDate = explode("-", $y[1]);
			$day =  $deptDate[2] + 1;
			$x = $y[0]. "=". $deptDate[0] . "-". $deptDate[1]."-". $day;
			$pieces[2] = $x;
			$url = implode( "&",$pieces);
		}
		if(($type) && (!$day)){
		// 1,0
		// enkeltje dag -1
			$x = $this->getCurrentUrl();
			$pieces = explode("&", $x);
			$deptDate = $pieces[2];
			$y =  explode("=", $deptDate);
			$deptDate = explode("-", $y[1]);
			$day =  $deptDate[2] - 1;
			$x = $y[0]. "=". $deptDate[0] . "-". $deptDate[1]."-". $day;
			$pieces[2] = $x;
			$url = implode( "&",$pieces);
		
		}
		
		if((!$type) && ($day)){
			//0,1 
			//retour dag +1
			$x = $this->getCurrentUrl();
			$pieces = explode("&", $x);
			$deptDate = $pieces[3];
			$y =  explode("=", $deptDate);
			$deptDate = explode("-", $y[1]);
			$day =  $deptDate[2] + 1;
			$x = $y[0]. "=". $deptDate[0] . "-". $deptDate[1]."-". $day;
			$pieces[3] = $x;
			$url = implode( "&",$pieces);
		
		}
		if((!$type) && (!$day)){
			//0,0
			//retour dag -1
			$x = $this->getCurrentUrl();
			$pieces = explode("&", $x);
			$deptDate = $pieces[3];
			$y =  explode("=", $deptDate);
			$deptDate = explode("-", $y[1]);
			$day =  $deptDate[2] - 1;
			$x = $y[0]. "=". $deptDate[0] . "-". $deptDate[1]."-". $day;
			$pieces[3] = $x;
			$url = implode( "&",$pieces);
		
		}
		

		return $url;
	}
}